#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@crane-pc>
#
# Distributed under terms of the MIT license.

"""

"""

from collections import Counter
import random
from math import factorial as fac


class BirthDay:
    def __init__(self, people_n=23, experience_n=10**4):
        self.people_n = people_n
        self.experience_n = experience_n
        self.share_same_cnt = 0

    def one_expe(self):
        ''' 进行一次试验: 并对试验结果进行记录 '''
        day_records = Counter()
        days = random.choices(range(365), k=self.people_n)
        for d in days:
            day_records[d] += 1
            if day_records[d] >= 2:
                self.share_same_cnt += 1
                return

    def expe(self):
        ''' 进行大量试验 '''
        for i in range(self.experience_n):
            self.one_expe()
        self.stats()

    def stats(self):
        ''' 对试验结果进行统计, 和概率比较 '''
        print('===================  BirthDay of [%s]=====================' % self.people_n)
        same_frequency = self.share_same_cnt / self.experience_n
        print('same frequencies %s' % same_frequency)

        prob = 1 - fac(365) / (fac(365-self.people_n) * (365 ** self.people_n))
        print('same        prob %s' % prob)


def main():
    print("start main")
    b = BirthDay(23)
    b.expe()

    b = BirthDay(30)
    b.expe()

    b = BirthDay(50)
    b.expe()

    b = BirthDay(60)
    b.expe()

    b = BirthDay(70)
    b.expe()

if __name__ == "__main__":
    main()
